<?php
require_once ROOT_ADMIN_MODELS.DS.'Models_Base.php';
class Models_Quest extends Models_Base{
	public function __construct(){
		parent::__construct();
		$this->_key="qid";
		$this->_table="quest";
	}
	
	public function listquest()
	{
		$sql="	SELECT *
				FROM quest
				";
		$data=$this->_db->fetchAll($sql,null,Zend_Db::FETCH_OBJ);
		foreach ($data as $key=>$row)
		{
			$sqlanswer="	SELECT *
					FROM answer
					WHERE qid =".$row->qid;
			$answer=$this->_db->fetchAll($sqlanswer,null,Zend_Db::FETCH_OBJ);
			$data[$key]->answer = $answer;
			
			$sqlnews="	SELECT news.headLines
					FROM quest_news,news
					WHERE  quest_news.news_id=news.id 
					AND q_id =".$row->qid;
			$news=$this->_db->fetchAll($sqlnews,null,Zend_Db::FETCH_OBJ);
			$data[$key]->news = $news;
		}
		return $data;
	}

	public function getquestByid($id)
	{
		$sql="	SELECT *
				FROM quest
				WHERE qid = $id";
		$data = $this->_db->fetchRow($sql);

		$sqlanswer="SELECT *
					FROM answer
					WHERE qid =".$data['qid'];
		$answer=$this->_db->fetchAll($sqlanswer,null,Zend_Db::FETCH_OBJ);
		$data['answer'] = $answer;
	
		$sqlnews="	SELECT news.headLines,news.id
					FROM quest_news,news
					WHERE  quest_news.news_id=news.id
					AND q_id =".$data['qid'];
		$news=$this->_db->fetchAll($sqlnews,null,Zend_Db::FETCH_OBJ);
		$data['news'] = $news;

		return $data;
	}

	public function updateAnswer($id,$qtitle)
	{
		$sql = "UPDATE quest SET qtitle = '$qtitle' WHERE qid = $id";
		$this->_db->query($sql);
	}

	public function insert($qtitle)
	{
		$sql = "INSERT INTO quest (qtitle,qdate) VALUES ('$qtitle',NOW())";
		$this->_db->query($sql);
		
		$sql = "SELECT MAX(qid) FROM quest";
		$count = $this->_db->fetchOne($sql);
		return $count;
	}
	/*
	public function del($id){
		$sql = "DELETE FROM typenews WHERE id = $id";
		$this->_db->query($sql);
	}
	
	public function countnews($id){
		$sql = "SELECT COUNT(id) FROM news WHERE typeNews_id = $id";
		$count = $this->_db->fetchOne($sql);
		return $count;
	}*/
}
